/************************************************************************************************
 * test examples of 100 interesting program in C
 * test 027.c
 * self-protected number
 ***********************************************************************************************/

#include <stdio.h>

/*
 *
 */

int main()
{
	int i = 0, cut_off = 10;
	for (i = 1; i <= 200000; i++)
	{
		if (i / cut_off > 0) cut_off *= 10;
		int j = 0, sum = 0, mul = 1, cut = 0;
		// cut before multiple, or overflow
		for (j = i, cut = cut_off; j > 0; j /= 10, mul *= 10, cut /= 10)
		{
			sum += (((j%10)*i)%cut) * mul;
		}
		if (i == (sum % cut_off))
			printf("%d is self-protected\n", i);
	}			
}

